package com.yt.controller;

import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@DefaultProperties(defaultFallback="defaultFallbackMes")
public class ProviderController {
    @Value("${server.port}")
    String prot;
    //可指定问题在下面类中
    HystrixCommandProperties p;
    @GetMapping("/mes/{ren}")
    @HystrixCommand
    public String getMes(@PathVariable("ren") int ren){

        float job=10.0f;
        int result=(int)(job/ren*1000);

//        if(result!=1){//一秒也忍不了
        try {
            TimeUnit.MILLISECONDS.sleep(result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        }

        return prot+":正常消息,10个任务/"+ren+"人，"+result+"天完成";
    }


    @GetMapping("/message/{ren}")
    @HystrixCommand(fallbackMethod = "getMessageFallback",commandProperties={
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    public String getMessage(@PathVariable("ren") int ren){

        float job=10.0f;
        int result=(int)(job/ren*1000);

//        if(result!=1){//一秒也忍不了
            try {
                TimeUnit.MILLISECONDS.sleep(result);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//        }

        return prot+":正常消息,10个任务/"+ren+"人，"+result+"天完成";
    }

    public String getMessageFallback(int ren){
        return prot+":出现问题，暂时无法访问。";
    }

    public String defaultFallbackMes(){
        return "默认的降级后使用的方法";
    }
}
